﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExcelUtilities.Impl
{
    /// <summary>
    /// read excel by epplus
    /// </summary>
    public class EPPlusExcelReader : ExcelReader
    {
        OfficeOpenXml.ExcelPackage excel;

        public EPPlusExcelReader()
        { }

        /// <summary>
        /// 打开xls文件
        /// </summary>
        /// <param name="xlsFile">xls文件</param>
        protected override void Open(string xlsFile)
        {
            excel = new OfficeOpenXml.ExcelPackage(new System.IO.FileInfo(xlsFile));
        }
        /// <summary>
        /// 打开数据流
        /// </summary>
        /// <param name="stream"></param>
        protected override void Open(System.IO.Stream stream)
        {
            excel = new OfficeOpenXml.ExcelPackage(stream);
        }
        /// <summary>
        /// 关闭
        /// </summary>
        protected override void Close()
        {
            excel.Dispose();
        }

        /// <summary>
        /// 获取最后一行的行号
        /// </summary>
        /// <param name="sheetOrder">第几个工作表（从1开始）</param>
        /// <returns>返回最后一行行号（从1开始）</returns>
        protected override Cfg.CellPosition GetEndPos(int sheetOrder)
        {
            var end = excel.Workbook.Worksheets[sheetOrder].Dimension.End;
            return new Cfg.CellPosition() { Row = end.Row, Col = end.Column };
        }
        /// <summary>
        /// 获取最后一行的行号
        /// </summary>
        /// <param name="sheetName">工作表表名</param>
        /// <returns>返回最后一行行号（从1开始）</returns>
        protected override Cfg.CellPosition GetEndPos(string sheetName)
        {
            var end = excel.Workbook.Worksheets[sheetName].Dimension.End;
            return new Cfg.CellPosition() { Row = end.Row, Col = end.Column };
        }

        /// <summary>
        /// 获取单元格的值
        /// </summary>
        /// <param name="sheetOrder">第几个工作表（从1开始）</param>
        /// <param name="cellAddress">单元格地址</param>
        /// <returns>返回单元格的值</returns>
        protected override object GetCellValue(int sheetOrder, string cellAddress)
        {
            return excel.Workbook.Worksheets[sheetOrder].Cells[cellAddress].Value;
        }
        /// <summary>
        /// 获取单元格的值
        /// </summary>
        /// <param name="sheetOrder">第几个工作表（从1开始）</param>
        /// <param name="row">单元格在第几行(从1开始)</param>
        /// <param name="col">单元格在第几列(从1开始)</param>
        /// <returns>返回单元格的值</returns>
        protected override object GetCellValue(int sheetOrder, int row, int col)
        {
            return excel.Workbook.Worksheets[sheetOrder].Cells[row, col].Value;
        }
        /// <summary>
        /// 获取单元格的值
        /// </summary>
        /// <param name="sheetName">工作表名</param>
        /// <param name="cellAddress">单元格地址</param>
        /// <returns>返回单元格的值</returns>
        protected override object GetCellValue(string sheetName, int row, int col)
        {
            return excel.Workbook.Worksheets[sheetName].Cells[row, col].Value;
        }
        /// <summary>
        /// 获取单元格的值
        /// </summary>
        /// <param name="sheetName">工作表名</param>
        /// <param name="row">单元格在第几行(从1开始)</param>
        /// <param name="col">单元格在第几列(从1开始)</param>
        /// <returns>返回单元格的值</returns>
        protected override object GetCellValue(string sheetName, string cellAddress)
        {
            return excel.Workbook.Worksheets[sheetName].Cells[cellAddress].Value;
        }
    }
}